import Vue from "vue";
import App from "./App";
import router from "./router";
import axios from "axios";
import elementUi from "element-ui";
import 'element-ui/lib/theme-chalk/index.css';


// 请求拦截 分析报错
axios.interceptors.response.use(
    response => {
        
        if(response.data.data === null){
            let params = {};
            params['url'] = response.config.url;
            if(response.config.method == 'get'){
                params = response.config.params;
            }else {
                params = response.config.data
            }
            
            axios.post('/api/uploadError', {message: JSON.stringify(params)}).then((json)=>{
                console.log(json)
            })
        }
        if(response.data && response.data.status == 401){
            return window.location.href = '/login'
        }
        return response;
    },
    error => {
        if (error.response) {
            switch (error.response.status) {
                case 401:
                    // 这里写清除token的代码
                    router.replace({
                        path: 'login',
                        query: {redirect: router.currentRoute.fullPath}//登录成功后跳入浏览的当前页面
                    })
            }
        }
        return Promise.reject(error.response.data) 
    });



Vue.prototype.$axios = axios;
// Vue.config.productionTip = false;

Vue.config.errorHandler = (err, vm, info) => {
    axios.post("/api/uploadError", { message: err.stack }).then(res => {
        console.log("UploadError In");
    });
};


Vue.use(elementUi)
// window.onerror = (msg,url,line, col, error) => {
//     let err = '';
//     err = `err:${msg},url:${url},lineNo:${line},${error}`;
//     axios.post('/api/uploadError', {message: err}).then((res)=>{
//         console.log(res)
//     })
//     return false
// }

new Vue({
    el: "#app",
    router,
    components: { App },
    template: "<App/>"
});
